$(function () {
    console.log("注册页面表单验证");
    var index1 = false;
    var index2 = false;
    var index3 = false;
    var index4 = false;
    var index5 = false;

    /* 验证手机号登录 */
    $("#user_phone").blur(function () {
        var str = $("#user_phone").val();
        var patrn = /^(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
        if (patrn.test(str) == false) {
            $("#user_phone_span").text("手机号输入有误").css("color", "red");
        } else {
            $.ajax({
                url: "../../loginRegistered/findByUserPhone",
                type: "post",
                dataType: "json",
                data: {
                    phone: str
                },
                success: function (map) {
                    var count = map.count;
                    if (count != 0) {
                        $("#user_phone_span").text("手机号重复").css("color", "red");
                    } else {
                        $("#user_phone_span").text("手机号输入正确").css("color", "green");
                        index1 = true;
                    }
                },
                error: function () {
                    window.location.href = "../../404.jsp";
                }
            });
        }
    });

    /*验证邮箱格式*/
    $("#user_email").blur(function () {
        var str = $("#user_email").val();
        var patrn = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        if (patrn.test(str) == false) {
            $("#user_email_span").text("邮箱输入格式有误").css("color", "red");
        } else {
            $.ajax({
                url: "../../loginRegistered/findByUserEmail",
                type: "post",
                dataType: "json",
                data: {
                    email: str
                },
                success: function (map) {
                    var count = map.count;
                    if (count != 0) {
                        $("#user_email_span").text("邮箱重复").css("color", "red");
                    } else {
                        $("#user_email_span").text("邮箱输入正确").css("color", "green");
                        index2 = true;
                    }
                },
                error: function () {
                    window.location.href = "../../404.jsp";
                }
            });
        }
    });

    /*密码格式验证*/
    $("#user_password").blur(function () {
        var str = $("#user_password").val();
        //var patrn = /^(?!\d+$)(?![A-Za-z]+$)(?![-.!@#$%^&*()+?><]+$)[a-zA-Z0-9-.!@#$%^&*()+?><]{6,}$/;
        var patrn = /^(\w){6,20}$/;
        if (patrn.test(str) == false) {
            $("#user_password_span").text("密码输入格式有误").css("color", "red");
        } else {
            $("#user_password_span").text("密码输入正确").css("color", "green");
            $("#password_prog1").text("弱").css("width", "");
            $("#password_prog2").text("较强").css("width", "");
            $("#password_prog3").text("强").css("width", "");
            if (str.length < 8) {
                $("#password_prog1").text("弱").css("width", "50%");
            } else if (str.length < 12) {
                $("#password_prog1").text("弱").css("width", "50%");
                $("#password_prog2").text("较强").css("width", "30%");
            } else {
                $("#password_prog1").text("弱").css("width", "50%");
                $("#password_prog2").text("较强").css("width", "30%");
                $("#password_prog3").text("强").css("width", "20%");
            }
            index3 = true;
        }
    });

    /*密码的强度显示*/
    $("#user_password").keyup(function () {
        var str = $("#user_password").val();
        $("#password_prog1").text("").css("width", "");
        $("#password_prog2").text("").css("width", "");
        $("#password_prog3").text("").css("width", "");
        if (str.length < 6) {
            $("#password_progress").removeClass();
        } else if (str.length < 8) {
            $("#password_progress").addClass("progress");
            $("#password_prog1").text("弱").css("width", "50%");
        } else if (str.length < 12) {
            $("#password_prog1").text("弱").css("width", "50%");
            $("#password_prog2").text("较强").css("width", "30%");
        } else {
            $("#password_prog1").text("弱").css("width", "50%");
            $("#password_prog2").text("较强").css("width", "30%");
            $("#password_prog3").text("强").css("width", "20%");
        }
    });

    /*确认密码*/
    $("#user_password2").blur(function () {
        var str = $("#user_password").val();
        var str2 = $("#user_password2").val();
        if (str != str2) {
            $("#user_password_span2").text("俩次输入的密码不一致").css("color", "red");
        } else {
            $("#user_password_span2").text("密码输入正确").css("color", "green");
            index4 = true;
        }
    });

    /*验证用户名不为空*/
    $("#user_name").blur(function () {
        var str = $("#user_name").val().trim();
        if (str == "") {
            $("#user_name_span").text("用户名不能为空").css("color", "red");
        } else if(str == "admin"){
            $("#user_name_span").text("用户名不能为(admin)").css("color", "red");
        }else{
            $("#user_name_span").text("用户名输入正确").css("color", "green");
            index5 = true;
        }
    });

    /*注册功能实现*/
    $("#btn_from").click(function () {
        console.log("注册功能" + $("#from_meaning_user").serialize())
        if (index1 == true && index2 == true && index3 == true && index5 == true && index5 == true) {
            $.ajax({
                url: "../../loginRegistered/addMeaningUser",
                data: $("#from_meaning_user").serialize(),
                type: "post",
                dataType: "json",
                success: function (data) {
                    window.location.href = "login.jsp";
                    index1 = false;
                    index2 = false;
                    index3 = false;
                    index4 = false;
                    index5 = false;
                },
                error: function () {
                    window.location.href = "../../404.jsp";
                }
            });
        } else {
            $("#zhuce_btn").text(">>请填写必填项").css("color", "red");
        }
    })
})